package santiago.view;

import java.awt.Image;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import santiago.domain.ColorType;
import santiago.domain.Plantation;
import santiago.domain.PlantationType;

public class ResourceManager {

    private static Map<String, Icon> PlantationResources;
    private static Map<String, Icon> YieldMarkerResources;
    private static Map<String, Image> PlantationResourcesImage;
    private static Map<String, Image> YieldMarkerResourcesImage;
    public static String FilePath = "/santiago/ressources/";

    public ResourceManager() {
        initPlantationResources();
        initYieldMarkerResources();
    }

    public static Icon getPlantationIcon(PlantationType Type, short Capacity, short YieldMarkersCount, ColorType PlayerColor) {
        String Color = "";
        if (PlayerColor != null) {
            Color = PlayerColor.toString();
        }
        return PlantationResources.get(Type.toString() + Capacity + YieldMarkersCount + Color);
    }

    public static Image getPlantationImage(PlantationType Type, short Capacity, short YieldMarkersCount, ColorType PlayerColor) {
        String Color = "";
        if (PlayerColor != null) {
            Color = PlayerColor.toString();
        }
        return PlantationResourcesImage.get(Type.toString() + Capacity + YieldMarkersCount + Color);
    }

    public static Image getPlantationImage(Plantation p) {
        return getPlantationImage(p.getType(), p.getCapacity(), p.getYieldMarkersCount(), p.getColor());
    }

    public static Icon getYieldMarkerIcon(ColorType Type) {
        return YieldMarkerResources.get(Type.toString());
    }

    private void initPlantationResources() {
        PlantationResources = new HashMap<>();
        PlantationResourcesImage = new HashMap<>();

        // Init Potato
        putResource(PlantationType.Potato + "0" + "0", "case-patate-vide.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "0", "case-patate-1-vide.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "1" + ColorType.WHITE, "case-patate-1-blanc.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "1" + ColorType.BEIGE, "case-patate-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "1" + ColorType.GRAY, "case-patate-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "1" + ColorType.PURPLE, "case-patate-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Potato + "1" + "1" + ColorType.BLACK, "case-patate-1-noir.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "0", "case-patate-2-vide.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "1" + ColorType.WHITE, "case-patate-1-blanc.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "2" + ColorType.WHITE, "case-patate-2-blanc.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "1" + ColorType.BEIGE, "case-patate-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "2" + ColorType.BEIGE, "case-patate-2-bois-clair.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "1" + ColorType.GRAY, "case-patate-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "2" + ColorType.GRAY, "case-patate-2-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "1" + ColorType.PURPLE, "case-patate-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "2" + ColorType.PURPLE, "case-patate-2-bordeau.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "1" + ColorType.BLACK, "case-patate-1-noir.png", PlantationResources);
        putResource(PlantationType.Potato + "2" + "2" + ColorType.BLACK, "case-patate-2-noir.png", PlantationResources);
        // Init Bean
        putResource(PlantationType.Bean + "0" + "0", "case-haricot-vide.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "0", "case-haricot-1-vide.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "1" + ColorType.WHITE, "case-haricot-1-blanc.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "1" + ColorType.BEIGE, "case-haricot-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "1" + ColorType.GRAY, "case-haricot-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "1" + ColorType.PURPLE, "case-haricot-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Bean + "1" + "1" + ColorType.BLACK, "case-haricot-1-noir.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "0", "case-haricot-2-vide.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "1" + ColorType.WHITE, "case-haricot-1-blanc.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "2" + ColorType.WHITE, "case-haricot-2-blanc.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "1" + ColorType.BEIGE, "case-haricot-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "2" + ColorType.BEIGE, "case-haricot-2-bois-clair.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "1" + ColorType.GRAY, "case-haricot-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "2" + ColorType.GRAY, "case-haricot-2-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "1" + ColorType.PURPLE, "case-haricot-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "2" + ColorType.PURPLE, "case-haricot-2-bordeau.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "1" + ColorType.BLACK, "case-haricot-1-noir.png", PlantationResources);
        putResource(PlantationType.Bean + "2" + "2" + ColorType.BLACK, "case-haricot-2-noir.png", PlantationResources);
        // Init Cannabis
        putResource(PlantationType.Cannabis + "0" + "0", "case-cannabis-vide.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "0", "case-cannabis-1-vide.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "1" + ColorType.WHITE, "case-cannabis-1-blanc.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "1" + ColorType.BEIGE, "case-cannabis-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "1" + ColorType.GRAY, "case-cannabis-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "1" + ColorType.PURPLE, "case-cannabis-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Cannabis + "1" + "1" + ColorType.BLACK, "case-cannabis-1-noir.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "0", "case-cannabis-2-vide.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "1" + ColorType.WHITE, "case-cannabis-1-blanc.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "2" + ColorType.WHITE, "case-cannabis-2-blanc.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "1" + ColorType.BEIGE, "case-cannabis-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "2" + ColorType.BEIGE, "case-cannabis-2-bois-clair.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "1" + ColorType.GRAY, "case-cannabis-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "2" + ColorType.GRAY, "case-cannabis-2-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "1" + ColorType.PURPLE, "case-cannabis-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "2" + ColorType.PURPLE, "case-cannabis-2-bordeau.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "1" + ColorType.BLACK, "case-cannabis-1-noir.png", PlantationResources);
        putResource(PlantationType.Cannabis + "2" + "2" + ColorType.BLACK, "case-cannabis-2-noir.png", PlantationResources);
        // Spice
        putResource(PlantationType.Spice + "0" + "0", "case-piment-vide.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "0", "case-piment-1-vide.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "1" + ColorType.WHITE, "case-piment-1-blanc.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "1" + ColorType.BEIGE, "case-piment-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "1" + ColorType.GRAY, "case-piment-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "1" + ColorType.PURPLE, "case-piment-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Spice + "1" + "1" + ColorType.BLACK, "case-piment-1-noir.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "0", "case-piment-2-vide.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "1" + ColorType.WHITE, "case-piment-1-blanc.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "2" + ColorType.WHITE, "case-piment-2-blanc.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "1" + ColorType.BEIGE, "case-piment-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "2" + ColorType.BEIGE, "case-piment-2-bois-clair.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "1" + ColorType.GRAY, "case-piment-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "2" + ColorType.GRAY, "case-piment-2-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "1" + ColorType.PURPLE, "case-piment-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "2" + ColorType.PURPLE, "case-piment-2-bordeau.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "1" + ColorType.BLACK, "case-piment-1-noir.png", PlantationResources);
        putResource(PlantationType.Spice + "2" + "2" + ColorType.BLACK, "case-piment-2-noir.png", PlantationResources);
        // Banana
        putResource(PlantationType.Banana + "0" + "0", "case-banane-vide.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "0", "case-banane-1-vide.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "1" + ColorType.WHITE, "case-banane-1-blanc.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "1" + ColorType.BEIGE, "case-banane-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "1" + ColorType.GRAY, "case-banane-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "1" + ColorType.PURPLE, "case-banane-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Banana + "1" + "1" + ColorType.BLACK, "case-banane-1-noir.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "0", "case-banane-2-vide.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "1" + ColorType.WHITE, "case-banane-1-blanc.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "2" + ColorType.WHITE, "case-banane-2-blanc.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "1" + ColorType.BEIGE, "case-banane-1-bois-clair.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "2" + ColorType.BEIGE, "case-banane-2-bois-clair.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "1" + ColorType.GRAY, "case-banane-1-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "2" + ColorType.GRAY, "case-banane-2-bois-fonce.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "1" + ColorType.PURPLE, "case-banane-1-bordeau.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "2" + ColorType.PURPLE, "case-banane-2-bordeau.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "1" + ColorType.BLACK, "case-banane-1-noir.png", PlantationResources);
        putResource(PlantationType.Banana + "2" + "2" + ColorType.BLACK, "case-banane-2-noir.png", PlantationResources);
    }

    private void initYieldMarkerResources() {
        YieldMarkerResources = new HashMap<>();
        YieldMarkerResourcesImage = new HashMap<>();

        putResource(ColorType.WHITE.toString(), "cube-blanc.png", YieldMarkerResources);
        putResource(ColorType.BEIGE.toString(), "cube-bois-clair.png", YieldMarkerResources);
        putResource(ColorType.GRAY.toString(), "cube-bois-fonce.png", YieldMarkerResources);
        putResource(ColorType.PURPLE.toString(), "cube-bordeau.png", YieldMarkerResources);
        putResource(ColorType.BLACK.toString(), "cube-noir.png", YieldMarkerResources);
    }

    private void putResource(String key, String FileName, Map<String, Icon> Resources) {
        Resources.put(key, new javax.swing.ImageIcon(getClass().getResource(FilePath + FileName)));
        try {
            if (Resources == PlantationResources) {
                PlantationResourcesImage.put(key, ImageIO.read(getClass().getResourceAsStream(ResourceManager.FilePath + FileName)));
            } else if (Resources == YieldMarkerResources) {
                YieldMarkerResourcesImage.put(key, ImageIO.read(getClass().getResourceAsStream(ResourceManager.FilePath + FileName)));
            }
        } catch (IOException ex) {
            Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
